package com.derbysoft.dswitch.adapter.ihg.web.action.support;

import com.derbysoft.dswitch.adapter.ihg.common.domain.Role;
import com.derbysoft.dswitch.adapter.ihg.common.domain.User;
import com.derbysoft.dswitch.adapter.ihg.common.repository.UserRepository;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Map;


public abstract class BaseAction extends ActionSupport implements SessionAware, Preparable {

    public static final String LIST = "list";
    public static final String ADD = "add";
    public static final String VIEW = "view";
    public static final String EDIT = "edit";

    public static final String SUCCESS = Action.SUCCESS;
    protected static final String KEY_CURRENT_USER = "CURRENT_USER";

    @Autowired
    protected UserRepository userRepository;

    protected Map sessionParams;

    @Override
    public void prepare() throws Exception {
    }

    public User getCurrentUser() {
        HttpSession session = ServletActionContext.getRequest().getSession();
        if (session.getAttribute(KEY_CURRENT_USER) != null) {
            return (User) session.getAttribute(KEY_CURRENT_USER);
        } else {
            String userName = SecurityContextHolder.getContext().getAuthentication().getName();
            if (StringUtils.isNotBlank(userName)) {
                User user = userRepository.findByName(userName);
                session.setAttribute(KEY_CURRENT_USER, user);
                return user;
            } else {
                User user = new User();
                user.setName(userName);
                user.setRoles(Arrays.asList(createRole("ANONYMOUS")));
                session.setAttribute(KEY_CURRENT_USER, user);
                return user;
            }
        }
    }

    private Role createRole(String roleName) {
        Role role = new Role();
        role.setName(roleName);
        return role;
    }

    protected Long getId() {
        String id = getParameter("id");
        if (StringUtils.isBlank(id)) {
            return null;
        }
        return new Long(id);
    }

    public void setSession(Map session) {
        this.sessionParams = session;
    }

    public Map getSessionParams() {
        return sessionParams;
    }

    protected String getParameter(String parameterName) {
        Object parameter = ActionContext.getContext().getParameters().get(parameterName);
        if (parameter == null) {
            return null;
        }
        if (String.class.isAssignableFrom(parameter.getClass())) {
            return parameter.toString();
        }
        Object[] parameters = (Object[]) parameter;
        if (ArrayUtils.isEmpty(parameters)) {
            return null;
        }
        return parameters[0].toString();
    }

    protected String[] getParameterValues(String parameterName) {
        String[] values = (String[]) ActionContext.getContext().getParameters().get(parameterName);
        if (ArrayUtils.isEmpty(values)) {
            return null;
        }
        return values;
    }

}